const dao = require('../dao/dao');
const dao_game = require('../dao/dao_game');

let rtp_free = 0.9;
let rtp_pay = 0.965;

let playerCoin = 0;  //玩家金币
let jiheTarget = 0;  //投注目标

let maxLeft = 3000;


exports.getPlayerRTP = async (playerId) => {
    let isMustWin = false; //是否必中大奖

    //获取是付费还是免费
    let payOrFreeData = await dao_game.getPlayerFreeOrPay(playerId)
    let isFree = 1;  // 是否免费
    if (payOrFreeData.respCode === 'SUCCESS') {
        if (payOrFreeData.data) {
            if (payOrFreeData.data.record) {
                isFree = payOrFreeData.data.record.isFree;
            } else {
                isFree = 1;
            }
            jiheTarget = payOrFreeData.data.totalDifference;
        }
    } else {
        return {
            respCode: 'FAIL',
        }
    }

    let RTP = 0;
    //获取是否中奖
    let RTPData = await dao_game.getPlayerTotalData(playerId, isFree)
    if (RTPData.respCode === 'SUCCESS') {
        playerCoin = RTPData.data.coin
        if ((!RTPData.data.total_bet_free && isFree) || (!RTPData.data.total_bet && !isFree)) {
            RTP = 0
        } else {
            if (isFree) {
                RTP = RTPData.data.total_win_free / RTPData.data.total_bet_free
            } else {
                RTP = RTPData.data.total_win / RTPData.data.total_bet
            }
        }
    } else {
        return {
            respCode: 'FAIL',
        }
    }

    let RTPmin = 0;
    if (isFree) {
        RTPmin = rtp_free
    } else {
        RTPmin = rtp_pay
    }

    // console.log(isFree);
    // console.log(RTP);
    // console.log(playerCoin);
    // console.log(jiheTarget);


    if (playerCoin < 200 && jiheTarget > maxLeft) {
        isMustWin = true;
    }


    return {
        respCode: 'SUCCESS',
        RTP,
        RTPmin,
        isFree,
        isMustWin
    }

}